package com.example.controller;

import com.example.service.DomainService;
import com.example.utils.JsonData;
import com.example.vo.DomainVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author yinxin
 * @version 1.0
 * @Description: 自定义短链
 * @date 2022/6/917:57
 */
@RestController
@RequestMapping("/api/domain/v1")
public class DomainController {

    @Autowired
    DomainService domainService;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    @GetMapping("/list")
    public JsonData listAll() {
        List<DomainVO> list = domainService.listAll();
        return JsonData.buildSuccess(list);
    }

/*    @GetMapping("/test")
    public JsonData test(@RequestPart(name = "code") String code, @RequestParam(name = "accounrNo") Long accountNo) {
        //key1是短链码，ARGV[1]是accountNo,ARGV[2]是过期时间
        String script =
                "if redis.call('EXISTS',KEYS[1])==0 " +
                "then redis.call('set',KEYS[1],ARGV[1]); " +
                "redis.call('expire',KEYS[1],ARGV[2]); " +
                "return 1;" +
                " elseif redis.call('get',KEYS[1]) == ARGV[1] then return 2;" +
                " else return 0; end;";
        Long result = redisTemplate.execute(new DefaultRedisScript<>(script, Long.class)
                ,Arrays.asList(code), accountNo, 100);
        return JsonData.buildSuccess(result);
    }*/
}
